#include <bits/stdc++.h>
#include "hutils.h"
#include "fz.h"

using namespace std;

int lnfz = -1, rnfz = -1; // 左右分子个数
int v[99], vm = 2;
Fz lfzs[99], rfzs[99];
time_t st, en;

void cv(int);

int main() {
    string inn;
    lnfz++;
    cout << ">";
    while (true) {
        cin >> inn;
        if (inn == "--") {
            break;
        }
        if (inn == "+") {
            lnfz++;
        }
        int fo = inn.length() - 1;
        for (int i = inn.length() - 1; i >= 0; i--) {
            if (isUp(inn[i])) {
                if (isNum(inn[fo])) {
                    if (isNum(inn[fo - 1])) { // 两位数
                        lfzs[lnfz].addYz(inn.substr(i, fo - i - 1), int(inn[fo - 1] - '0') * 10 + int(inn[fo] - '0'));
                    } else {
                        lfzs[lnfz].addYz(inn.substr(i, fo - i), int(inn[fo] - '0'));
                    }
                } else {
                    lfzs[lnfz].addYz(inn.substr(i, fo - i + 1), 1);
                }
                fo = i - 1;
            }
        }
    }

    rnfz++;
    while (true) {
        cin >> inn;
        if (inn == "!")
            break;
        if (inn == "+")
            rnfz++;
        int fo = inn.length() - 1;
        for (int i = inn.length() - 1; i >= 0; i--) {
            if (isUp(inn[i])) {
                if (isNum(inn[fo])) {
                    if (isNum(inn[fo - 1])) {
                        rfzs[rnfz].addYz(inn.substr(i, fo - i - 1), int(inn[fo - 1] - '0') * 10 + int(inn[fo] - '0'));
                    } else {
                        rfzs[rnfz].addYz(inn.substr(i, fo - i), int(inn[fo] - '0'));
                    }
                } else {
                    rfzs[rnfz].addYz(inn.substr(i, fo - i + 1), 1);
                }
                fo = i - 1;
            }
        }
    }

    cout << "Working..." << endl;
    st = clock();
    for (int i = 0; i <= lnfz + rnfz + 1; i++) {
        v[i] = 1;
    }

    while (true) {
        chk:
        // 赋值化学计量数
        for (int i = 0; i <= lnfz; i++) {
            lfzs[i].n = v[i];
        }
        for (int i = lnfz + 1; i <= lnfz + rnfz + 1; i++) {
            rfzs[i - lnfz - 1].n = v[i];
        }

        // chk
        {
            // 统计个原子个数
            Fz lyzs;
            for (int i = 0; i <= lnfz; i++) {
                for (int j = 0; j <= lfzs[i].nyz; j++) {
                    lyzs.addYz(lfzs[i].yzs[j].na, lfzs[i].yzs[j].n * lfzs[i].n);
                }
            }

            Fz ryzs;
            for (int i = 0; i <= lnfz; i++) {
                for (int j = 0; j <= rfzs[i].nyz; j++) {
                    ryzs.addYz(rfzs[i].yzs[j].na, rfzs[i].yzs[j].n * rfzs[i].n);
                }
            }

            // chk
            for (int i = 0; i <= lyzs.nyz; i++) {
                for (int j = 0; j <= ryzs.nyz; j++) {
                    if (lyzs.yzs[i].na == ryzs.yzs[j].na) {
                        if (lyzs.yzs[i].n != ryzs.yzs[j].n) {
                            cv(lnfz + rnfz + 1);
                            goto chk;
                        }
                    }
                }
            }

            // print
            {
                en = clock();
                for (int i = 0; i <= lnfz; i++) {
                    if (lfzs[i].n != 1) {
                        cout << lfzs[i].n;
                    }
                    for (int j = lfzs[i].nyz; j >= 0; j--) {
                        cout << lfzs[i].yzs[j].na;
                        if (lfzs[i].yzs[j].n != 1) {
                            cout << lfzs[i].yzs[j].n;
                        }
                    }
                    if (i != lnfz) {
                        cout << " + ";
                    }
                }
                cout << " == ";
                for (int i = 0; i <= rnfz; i++) {
                    if (rfzs[i].n != 1) {
                        cout << rfzs[i].n;
                    }
                    for (int j = rfzs[i].nyz; j >= 0; j--) {
                        cout << rfzs[i].yzs[j].na;
                        if (rfzs[i].yzs[j].n != 1) {
                            cout << rfzs[i].yzs[j].n;
                        }
                    }
                    if (i != rnfz) {
                        cout << " + ";
                    }
                }
                cout << endl << (en - st) / 1000 << "ms" << endl;
                exit(0);
            }
        }
    }
}

void cv(int x = lnfz + rnfz + 1) {
    if (x == 0 && v[0] == vm) {
        vm++;
        printf("vm: %d+1 = %d\n", vm - 1, vm);
        for (int i = 0; i < lnfz + rnfz + 1; i++) {
            v[i] = 1;
        }
        v[lnfz + rnfz + 1] = vm;
        return;
    }

    if (v[x] == vm) {
        v[x] = 1;
        cv(x - 1);
        return;
    }

    if (x != lnfz + rnfz + 1) { // 是进位
        v[x]++;
        for (int i = 0; i <= x; i++) {
            if (v[i] == vm) return;
        }
        v[lnfz + rnfz + 1] = vm;
        return;
    }
    v[x]++;
}